
clear
do "...\First.do"
********************************************************************************
use "$work\analysis_sample.dta"
keep if inrange(age,30,80)

g help=1 if inrange(timing,-4,0)
bys pnr: egen N_pre_2=total(help)
tab N_pre_2
keep if N_pre_2==5

drop if timing>3
drop if timing<-3

********************************************************************************
*** DDD
********************************************************************************
cap drop interaction2 interaction3 ddd3
g interaction2=low_ses*help3
g interaction3=low_ses*post
g ddd3=low_ses*help3*post

label var help3 "PCP low SES"
label var post "Post"
label var low_ses "Patient low SES"
label var interaction "PCP low SES x Post"
label var interaction2 "PCP low SES x Patient low SES"
label var interaction3 "Patient low SES x Post"
label var ddd3 "PCP low SES x Patient low SES x Post"


** Statins and CVC only for men
replace statins=. if male==0
replace death_CVC=. if male==0


******************************************************************************


sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)


sum death if  post==1 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe) cluster(newgpyr)

outreg2 using "$table\TableA1.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Death)

* Death CVC
sum death_CVC if post==1 & male==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death_CVC if  post==1 & low_ses==1 & male==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death_CVC if post==1 & help3==0 & male==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death_CVC help3 low_ses interaction2 $patient_X $gp_X if post==1 & male==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA1.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Death from CVC)




foreach var of varlist N_visits statins diabetes_control ACSC_COPD {

sum `var' if _unknown_new2==0 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if _unknown_new2==0 & post==0 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest `var' if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var' post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X, a(id year) cluster(id)  


outreg2 using "$table\TableA1.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')

}


